<?php
namespace app\home\controller;
use think\Controller;
use think\Db;
class Index extends Controller
{
    // 定义配置项
    private $config=array(
        'APPID'              => 'wxa7b1a16fe5e7191e', // 微信支付APPID.
        'MCHID'              => '1510804091', // 微信支付MCHID 商户收款账号  1556016481
        'KEY'                => 'dhgyQ67PG1xIveAiav5hCZySLV13ESW4', // 微信支付KEY  uniNCx3NaBq8yt7pFh4m1gY1dIqsALAy
        'APPSECRET'          => 'e7fa9fd1255a3460ad21324f8ae499a9', // 公众帐号secert (公众号支付专用)
        'NOTIFY_URL'         => 'http://app.panson.com/index/Index/huidiao', // 接收支付状态的连接
        'AUTH_URL'         => 'http://app.panson.com/home/Auth2/getuser', // 接收支付状态的连接

    );
    public function index()
    {
        $school = Db::name('wechat_school')->alias('ws')
            ->join('school s','ws.school=s.school_erp','LEFT')
            ->where('status', 1)
            ->group('school')
            ->field('school,school_name')
            ->select();
        $year = Db::name('wechat_school')
            ->where('status', 1)
            ->group('year')
            ->field('year')
            ->select();
        $this->assign([
            'school' => $school,
            'year' => $year,
        ]);
//        return $this->fetch('index/index');
        if(strpos($_SERVER['HTTP_USER_AGENT'],'MicroMessenger')!==false){
            if(!session('?openid22')){
                $authurl = $this->config['AUTH_URL'];
                $appid = config('wechat')['appid'];
                echo "<script> location.href='https://open.weixin.qq.com/connect/oauth2/authorize?appid={$appid}&redirect_uri={$authurl}&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect'; </script>";
                exit;
            }
            return $this->fetch('index/index');
        }else{
            echo "请在微信环境内访问！";
            exit();
        }
    }
    public function nextCan()
    {
        $school = $_POST['school'];
        $year = $_POST['year'];
        $school = Db::name('wechat_school')
            ->where('school', $school)
            ->where('year', $year)
            ->where('status', 1)
            ->find();
        if($school){
            return json(['status' => 200]);
        }else{
            return json(['status' => 100, 'msg' => '此学校没有此年份统购订单，请核实重新选择！']);
        }
    }

    /**
     * 采集信息提交
     * @return mixed|\think\response\Json
     */
    public function collect()
    {
        extract(input());
        if(request()->isPost()){
            $insert = [
                'openid' => session('openid22'),
                'orderno' => time() . rand(100000,999999),
                'tg_id' => $tg_id,
                'schoolerp' => $schoolerp,
                'school' => $school,
                'year' => $year,
                'grade' => $grade,
                'name' => $name,
                'sex' => $sex,
                'size' => $size,
                'add_time' => date('Y-m-d H:i:s'),
                'edit_time' => date('Y-m-d H:i:s')
            ];
            $insert['phone'] = $phone;
            if(!$grade || !$name || !$sex || !$size || !$phone){
                return json(['code' => 100, 'msg' => '有未填项！']);
            }
            if(!preg_match("/^1[123456789]\d{9}$/", $phone)){
                return json(['code' => 100, 'msg' => '请填写正确的手机号！']);
            }
            $have = Db::table('up_wechat_school')
                ->where('id',$tg_id)
                ->find();
            if($have['height'] == 1){
                $height = (int)$height;
                $insert['height'] = $height;
                if($height <= 0){
                    return json(['code' => 100, 'msg' => '身高不能为空且只能填写数字！']);
                }
            }
            if($have['weight'] == 1){
                $weight = round($weight,1);
                $insert['weight'] = $weight;
                if($weight <= 0){
                    return json(['code' => 100, 'msg' => '体重不能为空且只能填写数字！']);
                }
            }
            if($have['send'] == 1){
                $insert['send_name'] = $send_name;
                $insert['send_address'] = $send_address;
                if(!$send_name || !$send_address){
                    return json(['code' => 100, 'msg' => '有未填项！']);
                }
            }
            $res = Db::table('up_wechat_order')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '提交成功，请耐心等待发货~']);
            }else{
                return json(['code' => 100, 'msg' => '提交失败，请稍后重试~']);
            }
        }
        $tg = Db::name('wechat_school')
            ->where('school', $school)
            ->where('year', $year)
            ->where('status', 1)
            ->find();
        $sizes = [
            100,110,120,130,140,150,155,160
            ,165,170,175,180,185,190,195,200,205,210
            ,215,220,225
        ];
        $schname = Db::name('school')
            ->where('school_erp', $school)
            ->value('school_name');
        $this->assign([
            'schoolerp' => $school,
            'school' => $schname,
            'year' => $year,
            'tg' => $tg,
            'sizes' => $sizes,
        ]);
        return $this->fetch('index/info_collect');
    }
    /**
     * 我的订单
     */
    public function myOrder()
    {
        $data = Db::table('up_wechat_order')
            ->where('openid',session('openid22'))
            ->order('add_time desc')
            ->select();
        $this->assign([
            'data' => $data
        ]);
        return $this->fetch('index/my_order');
    }
    /**
     * 我的订单详情
     */
    public function orderInfo()
    {
        extract(input());
        $data = Db::table('up_wechat_order')
            ->where('o_id',$o_id)
            ->find();
        $this->assign([
            'data' => $data
        ]);
        return $this->fetch('index/order_xq');
    }
    /**
     * 我的物流列表
     */
    public function wuliuList()
    {
        extract(input());
        $data = Db::table('up_wechat_wuliu')
            ->where('w_o_id',$o_id)
            ->order('w_send_time desc')
            ->select();
        if($data){
            foreach ($data as $k => $v){
                $data[$k]['have'] = 1;
                $have = Db::table('up_wechat_huan')
                    ->where('h_w_id',$v['w_id'])
                    ->find();
                if($have) $data[$k]['have'] = 2;
            }
        }
        $this->assign([
            'data' => $data
        ]);
        return $this->fetch('index/wl_info');
    }
    /**
     * 我的物流详情
     */
    public function wuliuInfo()
    {
        extract(input());
        $data = Db::table('up_wechat_wuliu')
            ->where('w_id',$w_id)
            ->find();
        $wldata = orderWuLiu($data['w_express_no']);
        if($wldata['status'] != 200){
            $msg = [];
        }else{
            $msg = $wldata['msg'];
        }
        $wlstate = 0;
        if(isset($wldata['wlstate'])){
            $wlstate = $wldata['wlstate'];
        }
        $this->assign([
            'data' => $data,
            'msg' => $msg,
            'wlstate' => $wlstate,
        ]);
        return $this->fetch('index/logistics');
    }
    /**
     * 申请换货
     */
    public function askHuan()
    {
        extract(input());
        if(request()->isPost()){
            $insert = [
                'h_w_id' => $h_w_id,
                'h_o_id' => $h_o_id,
                'h_order_no' => time() . rand(100000,999999),
                'h_name' => $h_name,
                'h_size' => $h_size,
                'h_num' => $h_num,
                'h_reason' => $h_reason,
                'h_remark' => $h_remark,
                'h_express' => $h_express,
                'h_express_no' => $h_express_no,
                'h_add_time' => date('Y-m-d H:i:s')
            ];
            if(!$h_name || !$h_size || !$h_num || !$h_reason || !$h_express || !$h_express_no){
                return json(['code' => 100, 'msg' => '有未填项！']);
            }
            $res = Db::table('up_wechat_huan')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '申请售后成功，请耐心等待审核~']);
            }else{
                return json(['code' => 100, 'msg' => '申请售后失败，请稍后重试~']);
            }
        }
        $data = Db::table('up_wechat_wuliu')
            ->where('w_id',$w_id)
            ->find();
        $this->assign([
            'data' => $data
        ]);
        return $this->fetch('index/apply_service');
    }
    /**
     * 换货订单
     */
    public function huanList()
    {
        extract(input());
        $data = Db::table('up_wechat_huan')
            ->where('h_w_id',$w_id)
            ->order('h_add_time desc')
            ->select();
        $this->assign([
            'data' => $data
        ]);
        return $this->fetch('index/huan_list');
    }

}
